<?php


namespace app\admin\controller;


use app\service\cxyunService;
use app\service\orderService;
use think\facade\Db;
use think\db\Where;
use think\facade\View;

class Gate extends Common
{
    public function syncgate(){
        $id=request()->post("id");
        $info=Db::table("parkinglotinfo")->where("id",$id)->find();
        if(empty($info)){
            return json(['error'=>"停车场信息错误"]);
        }
        if(empty($info["cxyun_parking_id"])){
            return json(['error'=>"请填写cxyun车场编号"]);
        }

        $data["parkingId"]=$info["cxyun_parking_id"];
        $data["timestamp"]=time()."000";
        $cxyunApi=new cxyunService($info["cxyun_appkey"]);
        $res=$cxyunApi->commonPost("getParkGate",$data,array());
        $result= json_decode($res,true);
        if($result["status"]!=1){
            return json(['error'=>$result["message"]]);
        }
        $result= json_decode($result["Data"],true);
        $cxyunGateIdArr=array_column($result,"gateId");
        //查询现在系统的gateId
        $gateArr=Db::table("cxyun_parking_gate")
            ->where("parkingId",$info["cxyun_parking_id"])
            ->select();
        foreach ($gateArr as $value){
            if(!in_array($value["gateId"],$cxyunGateIdArr)){
                Db::table("cxyun_parking_gate")
                    ->where("id",$value["id"])
                    ->delete();
            }
        }

        $update["parkingNo"]=$info["parkingNo"];
        $update["parkingName"]=$info["parkingName"];

        foreach ($result as $gate){
            $update["parkingId"]=$info["cxyun_parking_id"];
            $update["gateId"]=$gate["gateId"];
            $update["gateName"]=$gate["gateName"];
            $update["gateNo"]=$gate["gateNo"];
            $update["iostate"]=$gate["iostate"];
            //查询数据中是否有该记录。有更新。没有新增
            $gateInfo=Db::table("cxyun_parking_gate")
                ->where("parkingId",$update["parkingId"])
                ->where("gateId",$update["gateId"])
                ->find();
            if(!empty($gateInfo)){
                Db::table("cxyun_parking_gate")->where("id",$gateInfo["id"])->update($update);
            }else{
                Db::table("cxyun_parking_gate")->insert($update);
            }
        }
        return json(['info' => "更新成功"]);
    }
    public function gatelist(){
        //根据当前账号查询区域
        $com = $this->pidTokenId();
        $whe=new Where;
        $where=new Where;
        $whe['id'] = array('in',$com['parking_id']);
        //根据账号查询停车场
        $parkinglist = (new orderService())->parkingList($whe);
        View::assign('parkinglist',$parkinglist);
        //查询停车场ID
        if(!empty($parkinglist)){
            foreach($parkinglist as $park){
                $parkingNo[] = $park['parkingNo'];
            }
            //将数组转换成字符串
            $parkingNo = implode(',',$parkingNo);
        }else{
            $parkingNo = null;
        }

        $gatelist =Db::name('cxyun_parking_gate')
            ->where('parkingNo','in',$parkingNo)
            ->select();
        $number = Db::name('cxyun_parking_gate')
            ->where('parkingNo','in',$parkingNo)
            ->count();
        View::assign('number',$number);
        View::assign('list',$gatelist);
        View::assign("domain_url",$this->getCurrentUrl());


        return View::fetch('gate_list');
    }
    public function gateedit(){
        if(request()->isPost()){
            $data=request()->post();
            //var_dump($data);exit;
            $res=Db::table("cxyun_parking_gate")->update($data);
            if($res){
                $array=Db::table("cxyun_parking_gate")
                    ->where("parkingId",$data["parkingId"])
                    ->where("isMonthlyStatus",1)
                    ->select();
                $gateArr=array_column($array, 'gateId');
                $gateId=implode(",",$gateArr);
                //var_dump($gateId);
                //exit;
                return "配置成功";
            }else{
                return "配置失败";
            }

        }else{

            $id=request()->get("id");
            $info=Db::table("cxyun_parking_gate")->where("id",$id)->find();
            if(empty($info)){
                return ;
            }
            View::assign('info',$info);
            return View::fetch("gate_edit");
        }
    }
}